package com.heima.admin.controller.v1;

import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 29143
 */
@Api(value = "频道管理",tags = "频道管理")
@RestController
@RequestMapping("/api/v1/channel")
public class AdChannelController {


    @Autowired
    private ChannelService channelService;

    /**
     * 模糊分页查询频道列表
     * @param channelDTO
     * @return
     */
    @ApiOperation("频道分页列表查询")
    @PostMapping("/list")
    public ResponseResult list(@RequestBody ChannelDTO channelDTO){
        return channelService.findByNameAndPage(channelDTO);
    }

    /**
     * 新增频道
     * @param adChannel
     * @return
     */
    @ApiOperation("新增频道")
    @PostMapping("/save")
    public ResponseResult add(@RequestBody AdChannel adChannel){
        return channelService.add(adChannel);
    }

    /**
     * 修改频道
     * @param adChannel
     * @return
     */
    @ApiOperation("修改频道")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody AdChannel adChannel){
        return channelService.update(adChannel);
    }

    /**
     * 删除频道
     * @param id
     * @return
     */
    @ApiOperation("删除频道")
    @GetMapping("/del/{id}")
    public ResponseResult deleteById(@PathVariable Integer id){
        //测试自定义异常
/*        if(true){
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
        }*/
        return channelService.deleteBuId(id);
    }

    @ApiOperation("查询全部频道")
    @GetMapping("/channels")
    public ResponseResult findAll() {
        //调用service中查询所有的方法
        List<AdChannel> list = channelService.list();
        return ResponseResult.okResult(list);
    }

    @ApiOperation("根据id查询频道")
    @GetMapping("/one/{id}")
    public ResponseResult findOne(@PathVariable Integer id) {
        return ResponseResult.okResult(channelService.getById(id));
    }
}
